package com.cdu.guoxinan.www.smartcom.controller;

import com.cdu.guoxinan.www.smartcom.domian.dto.ResidentQueryDTO;
import com.cdu.guoxinan.www.smartcom.domian.pojo.Resident;
import com.cdu.guoxinan.www.smartcom.domian.pojo.User;
import com.cdu.guoxinan.www.smartcom.service.ResidentService;
import com.cdu.guoxinan.www.smartcom.unit.model.R;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/resident")
@Api(tags = "居民信息管理")
public class ResidentController {
    @Resource
    private ResidentService residentService;

    @GetMapping("/queryPage")
    @ApiOperation(value = "分页查询居民信息")
    public R queryPage(ResidentQueryDTO residentQueryDTO){
        PageInfo<Resident> pageInfo = residentService.queryPage(residentQueryDTO);
        return R.ok(pageInfo);
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加居民信息")
    public R add(Resident resident){
        return residentService.add(resident)?R.ok("添加成功"):R.error("添加失败");
    }
    @PostMapping("/update")
    @ApiOperation(value = "修改居民信息")
    public R update(Resident resident){
        return residentService.update(resident)?R.ok("修改成功"):R.error("修改失败");
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除居民信息")
    public R delete(Integer id){
        return residentService.delete(id)?R.ok("删除成功"):R.error("删除失败");
    }

    /**
     * 根据用户ID查询居民信息
     */
    @GetMapping("/getByUserId")
    @ApiOperation(value = "根据用户ID查询居民信息")
    public R getByUserId(Long userId) {
        Resident resident = residentService.findByUserId(userId);
        if (resident == null) {
            return R.error(null);
        }
        return R.ok(resident);
    }
    @PostMapping("/setUser")
    @ApiOperation(value = "设置居民用户")
    public R setUser(User user, Integer residentId) {
        return residentService.setUser(user, residentId);
    }
}
